/*
 * Copyright (c) 2018-2999 北京小象智慧信息科技有限公司 All rights reserved.
 *
 * https://www.xiaoxiangai.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.yami.shop.api.utils.HttpResult;
import com.yami.shop.bean.model.User;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.google.common.collect.Maps;
import com.yami.shop.bean.app.param.SendSmsParam;
import com.yami.shop.bean.enums.SmsType;
import com.yami.shop.service.SmsLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/p/sms")
@Api(tags="发送验证码接口")
public class SmsController {

	@Autowired
	private SmsLogService smsLogService;


	@Autowired
    private UserService userService;

    /**
     * 发送验证码接口
     */
    @PostMapping("/send")
    @ApiOperation(value="发送验证码", notes="用户的发送验证码")
    public ResponseEntity<Boolean> audit(@RequestBody SendSmsParam sendSmsParam) {
		String userId = SecurityUtils.getUser().getUserId();
		Boolean bl =  smsLogService.sendSms(SmsType.REGISTRATIONCODE, sendSmsParam.getMobile(),Maps.newHashMap());

		return ResponseEntity.ok(bl);
    }

    @PostMapping("/sendSmsByUpdatingUserInformation")
    @ApiOperation(value="发送验证码", notes="用户的发送验证码")
    public HttpResult sendSms(@RequestBody SendSmsParam sendSmsParam) {
        String userId = SecurityUtils.getUser().getUserId();
        String mobile =  sendSmsParam.getMobile();
        User oldUser = userService.selectUserByPhoneOne(mobile);
        if(oldUser!=null){
            return HttpResult.failure("电话号码已经存在!");
        }
        Boolean bl =  smsLogService.sendSms(SmsType.REGISTRATIONCODE, sendSmsParam.getMobile(),Maps.newHashMap());

        return HttpResult.success();
    }

    /**
     * 验证邀请码是否正确
     *
     */
    @GetMapping("/validate")
    @ApiOperation(value="发送邀请码",notes="用户的邀请码")
    public ResponseEntity<String>  validate(String phonenum,String code){
        String userId = SecurityUtils.getUser().getUserId();
        String flag = smsLogService.checkValidCode(phonenum,code,SmsType.VALID);
        return  ResponseEntity.ok(flag);
    }

    /**
     * 测试发送短信给商家
     */
    @GetMapping("/sendSmsToMemberAfterConsumption")
    public ResponseEntity<Void> sendSmsToMemberAfterConsumption(){
        smsLogService.sendSmsToMemberAfterConsumption("18611128418","1369819498617638912");
        return ResponseEntity.ok().build();
    }

    @PostMapping("/loginBySendSms")
    @ApiOperation(value="发送验证码", notes="用户的发送验证码")
    public HttpResult loginBySendSms(@RequestBody SendSmsParam sendSmsParam) {
        String mobile =  sendSmsParam.getMobile();
        Boolean bl =  smsLogService.sendSms(SmsType.LOGINBYSENDSMS, sendSmsParam.getMobile(),Maps.newHashMap());

        return HttpResult.success();
    }

}
